package com.weimei.conversionTreasure.activity.compute;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;

import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.app.AppViewModelFactory;
import com.weimei.conversionTreasure.databinding.ActivityBzjdBinding;
import com.weimei.conversionTreasure.viewModel.BZJDVM;
import com.weimei.conversionTreasure.viewModel.SheRuVM;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.BR;

public class BzjdActivity extends BaseActivity<com.weimei.conversionTreasure.databinding.ActivityBzjdBinding, BZJDVM> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_bzjd;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        binding.back.setOnClickListener(v -> finish());

        // 限制输入的数字范围为1到100
        binding.edtJd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String input = s.toString();
                if (!input.isEmpty()) {
                    int number = Integer.parseInt(input);
                    if (number < 1 || number > 100) {
                        binding.edtJd.setText("");
                        ToastUtils.showShort("请输入1到100之间的数字");
                    }
                }
            }
        });
    }

    @Override
    public BZJDVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, (ViewModelProvider.Factory) factory).get(BZJDVM.class);
    }
}